package com.jch.dm.chapter_22_command.command;

import java.util.Iterator;
import java.util.Stack;

/**
 * @author: Administrator
 * “由多条命令整合成的命令”的类
 * @create: 2020/04/13 16:01
 **/
public class MacroCommand implements Command {

    /**
     * 命令的集合
     */
    private Stack commands = new Stack();

    /**
     * 执行
     */
    @Override
    public void execute() {
        Iterator it = commands.iterator();
        while(it.hasNext()){
            ((Command)it.next()).execute();
        }
    }

    /**
     * 添加命令
     */
    public void append(Command cmd){
        if (cmd != this) {
            commands.push(cmd);
        }
    }

    /**
     * 删除最后一条命令
     */
    public void undo(){
        if (!commands.empty()){
            commands.pop();
        }
    }

    /**
     * 删除所有命令
     */
    public void clear(){
        commands.clear();
    }
}
